﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TaskRepositoryTest.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Summary description for TeaskRepositoryTest
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Collaboris.DataAccess.Test
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;

    using Collaboris.DataAccess.SharePoint;
    using Collaboris.DataAccess.Test.Data_Entities;
    using Collaboris.DataAccess.Test.Repositories;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Summary description for TeaskRepositoryTest
    /// </summary>
    [TestClass]
    public class TaskRepositoryTest
    {
        private const string TaskListName = "TasksTest";
        
        public TaskRepositoryTest()
        {
            // TODO: Add constructor logic here
        }

        private TestContext testContextInstance;

        /// <summary>
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }

            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        #endregion

        /// <summary>
        /// Use TestInitialize to run code before running each test 
        /// </summary>
        [TestInitialize()]
        public void MyTestInitialize()
        {
            // create the test list
            using (SPSite site = new SPSite(TestGlobals.WssSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                   TestHelpers.CreateList(web, "Tasks", TaskListName, "Test Task List", true);
                }
            }
        }
        
        /// <summary>
        /// Use TestCleanup to run code after each test has run
        /// </summary>
        [TestCleanup()]
        public void MyTestCleanup()
        {
            // delete the test list
            using (SPSite site = new SPSite(TestGlobals.WssSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    //TestHelpers.DeleteList(web, TaskListName, false);
                }
            }
        }

        [TestMethod]
        public void Save_Load_Test()
        {
            using (SPSite site = new SPSite(TestGlobals.WssSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // create the tasks to add to the list
                    List<TaskInfo> tasksToSave = new List<TaskInfo>();
                    tasksToSave.Add(new TaskInfo(Priority.High, Status.Completed, "Task Title 1", DateTime.Now, web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Low, Status.InProgress, "Task Title 2", DateTime.Now, web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Normal, Status.Deferred, "Task Title 3", DateTime.Now, web.CurrentUser.LoginName));

                    // Create the repository
                    TaskRepository repository = new TaskRepository(web, TaskListName);
                    // Save the tasks
                    repository.Submit(tasksToSave);

                    // Now let's retrieve the tasks
                    List<TaskInfo> retrievedTasks = repository.LoadAllList();

                    Assert.IsTrue(tasksToSave[0] == retrievedTasks[0]);
                    Assert.IsTrue(tasksToSave[1] == retrievedTasks[1]);
                    Assert.IsTrue(tasksToSave[2] == retrievedTasks[2]);
                }
            }          
        }

        [TestMethod]
        public void SimpleQuery_Equals_Test()
        {
            using (SPSite site = new SPSite(TestGlobals.WssSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // create the tasks to add to the list
                    List<TaskInfo> tasksToSave = new List<TaskInfo>();
                    tasksToSave.Add(new TaskInfo(Priority.High, Status.Completed, "Task Title 1", DateTime.Now.AddDays(7), web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Low, Status.InProgress, "Task Title 2", DateTime.Now, web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Normal, Status.Deferred, "Task Title 3", DateTime.Now, web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Low, Status.Completed, "Task Title 4", DateTime.Now, web.CurrentUser.LoginName));
                    
                    // Create the repository
                    TaskRepository repository = new TaskRepository(web, TaskListName);
                    // Save the tasks
                    repository.Submit(tasksToSave);
                    
                    // Lets get the completed tasks
                    SPCriteria criteria = repository.CreateCriteria();
                    criteria.SimpleQuery = new QueryParam(
                        TaskRepository.FieldNames.Status, repository.StatusConverter.Convert(Status.Completed), "Choice");

                    List<TaskInfo> completedTasks = repository.LoadList(criteria);

                    Assert.IsTrue(completedTasks.Count == 2);
                    Assert.IsTrue(completedTasks[0] == tasksToSave[0]);
                    Assert.IsTrue(completedTasks[1] == tasksToSave[3]);
                }
            }
        }

        [TestMethod]
        public void SimpleQuery_Bigger_Test()
        {
            using (SPSite site = new SPSite(TestGlobals.WssSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // create the tasks to add to the list
                    List<TaskInfo> tasksToSave = new List<TaskInfo>();
                    tasksToSave.Add(new TaskInfo(Priority.High, Status.Completed, "Task Title 1", DateTime.Now, web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Low, Status.InProgress, "Task Title 2", DateTime.Now, web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Normal, Status.Deferred, "Task Title 3", DateTime.Now.AddDays(2), web.CurrentUser.LoginName));
                    tasksToSave.Add(new TaskInfo(Priority.Low, Status.Completed, "Task Title 4", DateTime.Now.AddDays(3), web.CurrentUser.LoginName));

                    // Create the repository
                    TaskRepository repository = new TaskRepository(web, TaskListName);
                    // Save the tasks
                    repository.Submit(tasksToSave);

                    // Lets get the  tasks due after today
                    SPCriteria criteria = repository.CreateCriteria();
                    criteria.SimpleQuery = new QueryParam(
                        TaskRepository.FieldNames.DueDate, SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now), "DateTime", ComparisonOperator.Greater);

                    List<TaskInfo> dueTasks = repository.LoadList(criteria);

                    Assert.IsTrue(dueTasks.Count == 2);
                    Assert.IsTrue(dueTasks[0] == tasksToSave[2]);
                    Assert.IsTrue(dueTasks[1] == tasksToSave[3]);
                   
                }
            }

        }




    }
}
